//
//  ViewController.m
//  TestScr
//
//  Created by 曹想 on 2019/1/22.
//  Copyright © 2019 曹想. All rights reserved.
//
#define KScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define KScreenHeight [[UIScreen mainScreen] bounds].size.height
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ZJScrollPageView/ZJScrollPageView.h"
#import <YYKit/YYKit.h>
#import <WebKit/WebKit.h>
@interface ViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong) UICollectionView * scr;

@property (nonatomic,strong) FirstViewController * firstVC;

@property (nonatomic,strong) SecondViewController * secondVC;

@property (nonatomic,copy) NSArray *titles;


@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {//kScreenWidth-70
    [super viewDidLoad];
    
    WKWebView * webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:webView];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
   
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://115.159.217.44/notice/api-index?notice_id=17"]];
    [webView loadRequest:request];
    
//    [web loadHTMLString:header baseURL:[NSURL URLWithString:@"http://115.159.217.44/notice/api-index?notice_id=17"]];
//    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
//    //显示遮盖
//    style.showCover = YES;
//    style.showLine = YES;
//    style.coverBackgroundColor = [UIColor colorWithHexString:@"38D6EF"];
//    style.scrollLineColor = [UIColor colorWithHexString:@"38D6EF"];
//
//    style.normalTitleColor = [UIColor colorWithHexString:@"333333"];
//    style.selectedTitleColor = [UIColor colorWithHexString:@"38D6EF"];
//    // 根据title总宽度自动调整位置 -- 达到个数少的时候会'平分'的效果, 个数多的时候就是可以滚动的效果 只有当scrollTitle == YES的时候才有效
//    //    style.autoAdjustTitlesWidth = YES;
//    // 不滚动标题
//    style.scrollTitle = NO;
//    // 同步调整遮盖或者滚动条的宽度 -- 只有当scrollTitle == NO的时候有效
//    style.adjustCoverOrLineWidth = YES;
//    // 颜色渐变
//    style.gradualChangeTitleColor = YES;
//    style.scaleTitle = NO;
//    self.titles = @[@"今日开服",
//                    @"即将开服",
//                    @"历史开服"
//                    ];
//    // 初始化
//
//   // ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-70, self.view.bounds.size.height - 34) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    
    //    scrollPageView.extraBtnOnClick = ^(UIButton *btn){
    //        NSLog(@"ffff");
    //    };
    //[self.view addSubview:scrollPageView];
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js,KScreenWidth];
    [webView evaluateJavaScript:js completionHandler:nil];
    [webView evaluateJavaScript:@"imgAutoFit()" completionHandler:nil];

}




-(UICollectionView *)scr{
    if (!_scr) {
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight);
        _scr = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth , KScreenHeight)];
        _scr.delegate = self;
        _scr.showsVerticalScrollIndicator = NO;
        _scr.showsHorizontalScrollIndicator = NO;
        _scr.bounces = NO;
        _scr.pagingEnabled = YES;
        _scr.contentSize = CGSizeMake(KScreenWidth * 2, KScreenHeight);
    }
    return _scr;
}

-(FirstViewController *)firstVC{
    if (!_firstVC) {
        _firstVC = [[FirstViewController alloc]init];
        _firstVC.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    }
    return _firstVC;
}


-(SecondViewController *)secondVC{
    if (!_secondVC) {
        _secondVC = [[SecondViewController alloc]init];
        _secondVC.view.frame = CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight);
    }
    return _secondVC;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == KScreenWidth) {
        [self addChildViewController:self.secondVC];
        [self.scr addSubview:self.secondVC.view];
    }
}




@end
